package com.nian.global.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 *
 * 哈希算法：SHA-256
 */
public class SHA256Utils {
    // 计算哈希值（十六进制字符串）
    public static String hash(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hashBytes = digest.digest(input.getBytes("UTF-8"));
            return Base64.getEncoder().encodeToString(hashBytes);
        } catch (Exception e) {
            throw new RuntimeException("SHA-256计算失败", e);
        }
    }

    // 带盐值的哈希
    public static String hashWithSalt(String input, String salt) {
        return hash(input + salt);
    }
}
